﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Data.NHHelper;
using DflyingForum.Core.Data.Domain.ValueObjects;

namespace Core.Web.Code.UI.Controls.ChildControls
{
    public partial class PostList : BaseControl
    {
        protected Repeater rptPostContent;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                GetDadaBind();
        }

        private void GetDadaBind()
        {
            if (TopicId > 0)
                _query.Add(new CriteriaParameter("TopicID", TopicId, CriteriaOperator.Eq));
            _query.Add(new CriteriaParameter("CreateTime", SelectQuery, CriteriaOperator.Ge));
            if (ModuleId > 0)
                _query.Add(new CriteriaParameter("Module.ID", ModuleId, CriteriaOperator.Eq));//关联查询
            if (StateId > 0)
                _query.Add(new CriteriaParameter("StateID", StateId, CriteriaOperator.Eq));
            _query.Add(new CriteriaParameter(SortBy, CriteriaOperator.OrderByDesc));//排序

            int recordCount;
            IList<Posts> result = _bllPost.FindByPage(1, 10, out recordCount, _query);

            rptPostContent.DataSource = result;
            rptPostContent.DataBind();
        }

        protected void rptPostContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Posts itemPost = e.Item.DataItem as Posts;
                if (itemPost != null)
                {
                    #region 绑定图片
                    Regex reg = new Regex(@"<img[\s\S]+>", RegexOptions.IgnoreCase);
                    if (reg.Match(itemPost.Body).Success)//喊有图片附件
                    {
                        Image Img_Attach = e.Item.FindControl("Img_Attach") as Image;
                        if (Img_Attach != null)
                        {
                            Img_Attach.Visible = true;
                            Img_Attach.ImageUrl = "~/Skins/MyTheme/images/post_img.gif";
                        }
                    }
                    if (_bllPost.HowToGetHotPost(itemPost.ID))//热帖
                    {
                        Image Img_Hot = e.Item.FindControl("Img_Hot") as Image;
                        if (Img_Hot != null)
                        {
                            Img_Hot.Visible = true;
                            Img_Hot.ImageUrl = "~/Skins/MyTheme/images/post_hot.gif";
                        }
                    }
                    if (_bllPost.IsExcellent(itemPost))
                    {
                        Image Img_Digest = e.Item.FindControl("Img_Digest") as Image;
                        if (Img_Digest != null)
                        {
                            Img_Digest.Visible = true;
                            Img_Digest.ImageUrl = "~/Skins/MyTheme/images/post_digest.gif";
                        }
                    }
                    if (true)//帖子被加分，算法待定
                    {
                        Image Img_Agree = e.Item.FindControl("Img_Agree") as Image;
                        if (Img_Agree != null)
                        {
                            Img_Agree.Visible = true;
                            Img_Agree.ImageUrl = "~/Skins/MyTheme/images/post_score.gif";
                        }
                    }
                    #endregion

                    Label lblReplyTime = e.Item.FindControl("lblReplyTime") as Label;
                    Label lblReplyUser = e.Item.FindControl("lblReplyUser") as Label;

                    User u = _bllUser.FindBy(itemPost.LastReplyUserID);
                    if (u != null)
                        lblReplyUser.Text = u.NickName;
                    else
                        lblReplyUser.Text = itemPost.PubUser.NickName;

                    Reply lastReply = _bllReply.FindBy(itemPost.LastReplyID);
                    if (lastReply != null)
                        lblReplyTime.Text = lastReply.CreateTime.ToString("yyyy-MM-dd hh:mm");
                    else
                        lblReplyTime.Text = itemPost.CreateTime.ToString("yyyy-MM-dd hh:mm");
                }
            }
        }

        //获取普通主题(非置顶)标题前显示的图片
        protected string GetImg(object oId)
        {
            int postId = Convert.ToInt32(oId);
            Posts p = _bllPost.FindBy(postId);
            IList<PostState> pss = p.PostStates;
            foreach (var item in pss)
            {
                if (EnumPostState.Lock == (EnumPostState)item.Flag)
                    return "Skins/MyTheme/images/post_lock.gif";
            }
            return "Skins/MyTheme/images/post_common.gif";
        }

        #region 外部需要赋值字段
        //需要获取 分类、模块、排序{默认、发布时间、回复时间、点击次数、回复次数}、
        //筛选{（全部、今天、两天、一周、一个月、三个月）}、状态{精华、推荐}
        public int TopicId { get; set; }
        public int ModuleId { get; set; }
        public string SortBy { get; set; }
        public DateTime SelectQuery { get; set; }
        public int StateId { get; set; }
        #endregion
    }
}